//
//  ViewController.m
//  MSQScreenCapture
//
//  Created by 毛韶谦 on 2017/7/26.
//  Copyright © 2017年 毛韶谦. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *baseImageView;

@property (weak, nonatomic) IBOutlet UIImageView *bottonImageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
#pragma mark --- 方法一 ----
    
    /**
     创建一个基于位图的上下文（context）,并将其设置为当前上下文(context)
     
     @param size 参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小
     @param opaque 透明开关，如果图形完全不用透明，设置为YES以优化位图的存储。
     @param scale 缩放因子 iPhone 4是2.0，其他是1.0。虽然这里可以用[UIScreen mainScreen].scale来获取，但实际上设为0后，系统就会自动设置正确的比例了
     */
    UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size, YES, 0.0);
    UIView * view = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
    //把控制器View的内容绘制到上下文当中.
    //layer是不能够直接绘制的.要用渲染的方法才能够让它绘制到上下文当中。UIGraphicsGetCurrentContext()
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    //从上下文当中生成一张图片
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文.
    UIGraphicsEndImageContext();
    /*
     //image:要把图片转成二进制流，compressionQuality:可压缩质量.
     NSData*data =UIImagePNGRepresentation(viewImage);
     */
    
    //上面我们获得了一个全屏的截图，下边的方法是对这个图片进行裁剪。
    CGImageRef imageRef =viewImage.CGImage;
    //这里要特别注意，这里的宽度 CGImageGetWidth(imageRef) 是图片的像素宽（高度同解），所以计算截图区域时需要按比例来；
    //这里举的例子是宽高屏幕1/2位置在中心
    CGRect rect =  CGRectMake(CGImageGetWidth(imageRef)/4, CGImageGetHeight(imageRef)/2-CGImageGetWidth(imageRef)/2, CGImageGetWidth(imageRef)/2, CGImageGetWidth(imageRef)/2);//这里可以设置想要截图的区域
    
    CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
    
    UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];

    
#pragma mark --- 方法二 ----
    
    //设置边框宽度
    CGFloat borderWH = 5;
    //加载图片
    CGFloat imageWH = self.baseImageView.image.size.width;
    NSLog(@"=========%f",imageWH);
    
    //开启一个位图上下文(W = imageWH + 2 * border H = imageWH + 2 * border)
    CGSize size =CGSizeMake(imageWH + 2*borderWH, imageWH + 2*borderWH);
    
    UIGraphicsBeginImageContextWithOptions(size,NO,0);
    
    //绘制一个圆形的形状.
    UIBezierPath*path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0, size.width, size.height)];
    
    //边框颜色
    UIColor *color = [UIColor redColor];
    [color set];
    
    [path fill];
    
    //设置一个小圆,并设置成裁剪区域
    path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWH, borderWH, imageWH, imageWH)];
    
    //把路径设置成裁剪区域
    [path addClip];
    
    //把图片绘制到上下文当中.
    [self.baseImageView.image drawAtPoint:CGPointMake(borderWH, borderWH)];
    
    //从上下文当中生成一张图片
    UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭上下文.
    UIGraphicsEndImageContext();
    
    self.bottonImageView.image = newImage;
    
    
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
